package com.itheima.controller.backend;

import com.itheima.annotation.FlashFTL;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.enumeration.OperationType;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
//@Controller
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @FlashFTL(type = OperationType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        Boolean success = setmealService.add(setmeal, checkgroupIds);

        if(success){
            log.info("新增套餐成功{}", setmeal);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        }else {
            log.info("新增套餐失败{}", setmeal);
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 根据id删除套餐
     * @param id
     * @return
     */
    @FlashFTL(type = OperationType.DELETE)
    @GetMapping("/deleteById")
    public Result deleteById(Integer id){
        Boolean success = setmealService.deleteById(id);
        if(success){
            log.info("删除套餐成功{}", id);
            return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
        }else {
            log.info("删除套餐失败{}", id);
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    /**
     * 编辑套餐
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @FlashFTL(type = OperationType.UPDATE)
    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkGroupIds){
        Boolean success = setmealService.edit(setmeal, checkGroupIds);

        if(success){
            log.info("编辑套餐成功{}", setmeal);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        }else {
            log.info("编辑套餐失败{}", setmeal);
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal = setmealService.findById(id);
        boolean success = setmeal != null;
        if (success){
            log.info("查询套餐成功{}", setmeal);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        }else {
            log.info("查询套餐失败{}", id);
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }

    }

    /**
     * 查询所有套餐
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmealService.findAll());
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
//    @FlashFTL(type = OperationType.FLASH)
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.findPage(queryPageBean);
    }

    /**
     * 上传图片
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        return setmealService.upload(imgFile);
    }
}
